<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>数据库表列表</title>
		<link rel="stylesheet" href="../../component/pear/css/pear.css" />
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">表名称</label>
							<div class="layui-input-inline">
								<input type="text" name="name" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="gen-query">
								<i class="layui-icon layui-icon-search"></i>查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md">
								<i class="layui-icon layui-icon-refresh"></i>重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="gen-table" lay-filter="gen-table"></table>
			</div>
		</div>

		<script type="text/html" id="operationTpl">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="generate"><i class="layui-icon layui-icon-play"></i>生成代码</button>
		</script>

		<script type="text/html" id="havePrimaryKeyTpl">
			{{# if (d.havePrimaryKey) { }}
		    <span>是</span>
		    {{# } else { }}
		    <span>否</span>
		    {{# } }}
		</script>

		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery', 'admin'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let admin = layui.admin;


				let cols = [
					[
						{ type: 'checkbox', hide: true },
                        { field: 'name', title: '表名称', width: 300 },
                        { field: 'havePrimaryKey', title: '是否有主键', width: 100, templet: '#havePrimaryKeyTpl' },
                        { field: 'fieldNames', title: '所有字段', minWidth: 200 },
                        { field: 'comment', title: '表注释', width: 200 },
                        { field: 'operation', title: '操作', width: 130, fixed: 'right', templet: '#operationTpl' }
					]
				]

				table.render({
					elem: '#gen-table',
					url: '/vtl-generator/table/list',
					page: false,
					cols: cols,
					skin: 'line',
					toolbar: '#gen-toolbar',
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});

				table.on('toolbar(gen-table)', function(obj) {
					if (obj.event === 'refresh') {
						window.refresh();
					}
				});

				table.on('tool(gen-table)', function(obj) {
					var row = obj.data;
					if (obj.event === 'generate') {
						window.generate(row.name)
					}
				});

				form.on('submit(gen-query)', function(data) {
					window.refresh(data.field)
					return false;
				});

				window.refresh = function(field = {}) {
					table.reload('gen-table', { where: field })
				}

				window.generate = function(tableName) {
					top.layui.admin.jump(tableName, '生成代码【' + tableName + '】', 'views/generator/detail.html?name=' + tableName)
				}

			})
		</script>
	</body>
</html>
